﻿@page "/sys/openapiuser"
@using System.Linq.Expressions;
@inject MasaBlazor MasaBlazor
@inject IOpenApiUserService OpenApiUserService
@namespace ThingsGateway.Web.Rcl
@attribute [Authorize]
@inject UserResoures UserResoures
@attribute [RolePermission]
@inherits CultureComponentBase
@layout MainLayout
<AppDataTable @ref="_datatable" TItem="OpenApiUser" SearchItem="OpenApiUserPageInput" AddItem="OpenApiUserAddInput" EditItem="OpenApiUserEditInput"
              SearchModel="@search" Dense 
              QueryCall="QueryCall" AddCall="AddCall" DeleteCall="DeleteCall" EditCall="EditCall"
              FilterHeaders="FilterHeaders" Filters="Filters"
              ShowQueryButton IsShowSearchKey ShowDetailButton
              ShowAddButton=@UserResoures.IsHasButtonWithRole("openapiuseradd")
              ShowDeleteButton=@UserResoures.IsHasButtonWithRole("openapiuserdelete")
              ShowEditButton=@UserResoures.IsHasButtonWithRole("openapiuseredit")>

    <AddTemplate>

        <MSubheader Class="mt-4 font-weight-black"> @(context.Description(x => x.Account))  </MSubheader>
        <MTextField Dense Outlined HideDetails="@("auto")" @bind-Value=@context.Account />

        <MSubheader Class="mt-4 font-weight-black"> @(context.Description(x => x.Password))  </MSubheader>
        <MTextField Dense Outlined HideDetails="@("auto")" @bind-Value=@context.Password />

        <MSubheader Class="mt-4 font-weight-black">   @(context.Description(x => x.Phone))</MSubheader>
            <MTextField Dense Outlined HideDetails="@("auto")" @bind-Value=@context.Phone />

            <MSubheader Class="mt-4 font-weight-black">    @(context.Description(x => x.Email))  </MSubheader>
        <MTextField Dense Outlined HideDetails="@("auto")" @bind-Value=@context.Email />

        <MSubheader Class="mt-4 mb-5 font-weight-black">@(context.Description(x => x.SortCode)) </MSubheader>
        <MSlider @bind-Value=@context.SortCode Class="mb-5" TValue=int ThumbLabel="@("always")" Dense />

    </AddTemplate>

    <EditTemplate>
        <MSubheader Class="mt-4 font-weight-black"> @(context.Description(x => x.Account))  </MSubheader>
        <MTextField Dense Outlined HideDetails="@("auto")" @bind-Value=@context.Account />

        <MSubheader Class="mt-4 font-weight-black"> @(context.Description(x => x.Password))  </MSubheader>
        <MTextField Dense Outlined HideDetails="@("auto")" @bind-Value=@context.Password />

        <MSubheader Class="mt-4 font-weight-black">   @(context.Description(x => x.Phone))</MSubheader>
            <MTextField Dense Outlined HideDetails="@("auto")" @bind-Value=@context.Phone />

            <MSubheader Class="mt-4 font-weight-black">    @(context.Description(x => x.Email))  </MSubheader>
        <MTextField Dense Outlined HideDetails="@("auto")" @bind-Value=@context.Email />

        <MSubheader Class="mt-4 mb-5 font-weight-black">@(context.Description(x => x.SortCode)) </MSubheader>
        <MSlider @bind-Value=@context.SortCode Class="mb-5" TValue=int ThumbLabel="@("always")" Dense />
    </EditTemplate>

    <ItemColOperTemplate>
        <MList Dense>
            @if (@UserResoures.IsHasButtonWithRole("openapiuserper"))
            {
                <MListItem OnClick="async()=>
{
ChoiceUserId=context.Item.Id;
RolesChoice=GetRouters().TreeToList().Where(it=>context.Item.PermissionCodeList?.Contains(it.ApiRoute)==true).ToList() ;
IsShowRoles=true;
}">
                    <MListItemTitle Class="ml-2"> @T("Api权限") </MListItemTitle>
                </MListItem>
            }

        </MList>
    </ItemColOperTemplate>

    <ItemColTemplate>
        @switch (context.Header.Value)
        {

            case nameof(context.Item.UserStatus):
                <EnableSwitch ValueChanged=@(a=>UserStatusChange(context.Item,a))
                      Value="context.Item.UserStatus">
                </EnableSwitch>
                break;
            default:
                @context.Value
                break;

        }

    </ItemColTemplate>
</AppDataTable>

<PDrawer @bind-Value="IsShowRoles" OnCancel="() => IsShowRoles = false"
         Title=@T("Api权限")
         Width=@(MasaBlazor.Breakpoint.Mobile?"100%":"500")
         MaxWidth="500" OnSave="OnRolesSave">
    @if (IsShowRoles)
    {
        <MCard Flat Class="ma-0 pa-4">
            <MCardTitle Class="py-2">
                <MTextField Clearable Dense Style="max-width:200px;" HideDetails=@("auto") Class="my-1 mx-2 ml-3" @bind-Value="SearchName"
                        Outlined Label=@typeof(OpenApiPermissionTreeSelector).GetDescription(nameof(OpenApiPermissionTreeSelector.ApiName)) />
            </MCardTitle>
            <MTreeview Class="my-1" Search="@SearchName" Dense OpenAll TItem="OpenApiPermissionTreeSelector" TKey="OpenApiPermissionTreeSelector" Selectable @bind-Value="RolesChoice" Items="AllRouters"
                   ItemText="r=>r.PermissionName" ItemChildren="r=>r.Children"
                   ItemKey=@(r=>r)>
            </MTreeview>
        </MCard>

    }

</PDrawer>

@code {
    string SearchName;
    long ChoiceUserId;
    [Inject]
    IRoleService SysRoleService { get; set; }
    bool IsShowRoles;
    List<OpenApiPermissionTreeSelector> RolesChoice = new();
    private async Task OnRolesSave(ModalActionEventArgs args)
    {
        try
        {
            OpenApiUserGrantPermissionInput userGrantRoleInput = new();
            userGrantRoleInput.Id = ChoiceUserId;
            userGrantRoleInput.PermissionList = RolesChoice.Select(it => it.ApiRoute).ToList();
            await OpenApiUserService.GrantRole(userGrantRoleInput);
            IsShowRoles = false;
            await _datatable?.QueryClick();
        }
        catch (Exception ex)
        {
            args.Cancel();
            await PopupService.EnqueueSnackbarAsync(ex,false);
        }
    }

    private List<OpenApiPermissionTreeSelector> AllRouters;

    private List<OpenApiPermissionTreeSelector> GetRouters()
    {
        AllRouters = ApplicationExtension.OpenApiPermissionTreeSelector().ToList();
        return AllRouters;
    }
}